package leetcode;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0033.java
 * @Author : WangRuoyu
 * @Date 2023/11/1 16:40
 */


public class Q0033 {
    public int search(int[] nums, int target) {
        int l = 0, r = nums.length - 1;
        if (nums[0] > nums[nums.length - 1]) {
            int i = 0, j = nums.length - 1;
            while (i < j) {
                int mid = i + j >> 1;
                if (nums[mid] < nums[0]) j = mid;
                else i = mid + 1;
            }
            if (target >= nums[0]) r = i - 1;
            else l = i;
        }

        while (l < r) {
            int mid = l + r >> 1;
            if (nums[mid] >= target) r = mid;
            else l = mid + 1;
        }

        return target == nums[l] ? l : -1;
    }

    public static void main(String[] args) {
        Q0033 sl = new Q0033();
        int nums[] = {3, 1};
        int target = 3;
        System.out.println(sl.search(nums, target));
    }
}
